'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/dls/kh/cwdz/tongji 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		const db = uniCloud.database();
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			uid,
			todayStart,
			StartTime,
			EndTime
		} = data;
		let res = {
			code: 0,
			msg: "",
			yjyj: 0,
			yjbl: 0
		};
		// 业务逻辑开始-----------------------------------------------------------

		if (userInfo.role.includes("admin")) {
			//查询今日充值总金额
			res.jrczje = await vk.baseDao.sum({
				dbName: "a_recharge", // 表名
				fieldName: "shifu_amount", // 需要求和的字段名
				whereJson: {
					//支付状态必须是已支付
					pay_status: 1,
					_add_time: _.and(_.gt(StartTime), _.lt(EndTime)),
				}
			});
			return res;
		}

		if (userInfo.role.includes("middleman")) {
			//查询今日充值总金额
			res.jrczje = await vk.baseDao.sum({
				dbName: "a_recharge", // 表名
				fieldName: "shifu_amount", // 需要求和的字段名
				whereJson: {
					middleman_id: uid,
					//支付状态必须是已支付
					pay_status: 1,
					_add_time: _.and(_.gt(StartTime), _.lt(EndTime)),
				}
			});
			return res;
		}

		if (userInfo.role.includes("agent")) {
			res.jrczje = await vk.baseDao.sum({
				dbName: "a_recharge", // 表名
				fieldName: "shifu_amount", // 需要求和的字段名
				whereJson: {
					invite_code: uid,
					//支付状态必须是已支付
					pay_status: 1,
					_add_time: _.and(_.gt(StartTime), _.lt(EndTime)),
				}
			});
			return res;
		}

		if (userInfo.agent_id) {
			let agent_grade = await db.collection('a_agent_grade').doc(userInfo.agent_id).get()
			//查询当前代理商的佣金比例，计算预计佣金
			res.yjyj = agent_grade.data[0].commission_rate * res.jrczje
			// 佣金比例
			res.yjbl = agent_grade.data[0].commission_rate
		}


		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}